package com.jamin.aabill.ui;

import java.util.List;

import com.jamin.aabill.R;
import com.jamin.aabill.custom.TopBar;
import com.jamin.aabill.custom.TopBarClickListener;
import com.jamin.aabill.data.BillViewDetail;
import com.jamin.aabill.utils.TimeUtils;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class BillDetailActivity extends Activity{

	TopBar mTopBar = null;
	TextView mCreateTimeView = null;
	TextView mPayNameView = null;
	TextView mProfitNameView = null;
	TableLayout mItemLayout = null;
	long billId = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.bill_detail_activity);
		billId = getIntent().getLongExtra("billId", -1);
		initView();
		bindView();

	}


	private void initView() {
		mTopBar = (TopBar) findViewById(R.id.top_bar);
		mTopBar.setTopBarClickListener(new TopBarClickListener() {
			@Override
			public void rightBtnClick() {
			}
			@Override
			public void right2BtnClick() {
			}
			@Override
			public void leftBtnClick() {
				finish();
			}
		});
		mCreateTimeView = (TextView) findViewById(R.id.bill_detail_activity_create_time);
		mPayNameView = (TextView) findViewById(R.id.bill_detail_activity_pay_name);
		mProfitNameView = (TextView) findViewById(R.id.bill_detail_activity_profit_name);
		mItemLayout = (TableLayout) findViewById(R.id.bill_detail_activity_item);
	}




	/**
	 * 装载数据<br>
	 * @method bindView
	 * @throws 
	 * @since v1.0
	 */
	private void bindView() {
		List<BillViewDetail> list = AABillApplication.mBillManager.getBillViewDetailForBill(billId);
		if(list.size() > 0){
			mTopBar.setTitle(list.get(0).getPROJECT());
			mCreateTimeView.setText(AABillApplication.mApplicationContext.getString(R.string.bill_detail_activity_create_time,
					TimeUtils.FormatTimeForm(list.get(0).getCREATE_AT() , "MM-dd")));
			mPayNameView.setText(bindPayName(list));
			mProfitNameView.setText(bindProfitName(list));
		}
		mItemLayout = (TableLayout) findViewById(R.id.bill_detail_activity_item);
		mItemLayout.setStretchAllColumns(true);
		mItemLayout.setShrinkAllColumns(true);
		String MemberNameTitle = AABillApplication.mApplicationContext.getString(R.string.bill_detail_activity_member_name);
		String MemberCostTitle = AABillApplication.mApplicationContext.getString(R.string.bill_detail_activity_member_cost);
		mItemLayout.addView(getTableRow( MemberNameTitle , MemberCostTitle ));
		for(int i = 0 ; i < list.size(); i++){
			mItemLayout.addView(getTableRow(list.get(i).getCIRCLE_MEMBER_NAME(), String.valueOf(list.get(i).getMONEY())));
		}
	}


	private TableRow getTableRow(String name , String cost){
		TableRow row = new TableRow(this);
		row.addView(getTextView(name));
		row.addView(getTextView(cost));
		return row;
	}


	private TextView getTextView(String name){
		TextView tv = new TextView(this);
		tv.setTextColor(R.color.blue);
		tv.setText(name);
		tv.setBackgroundResource(R.color.item_bg);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
		tv.setGravity(Gravity.CENTER);
		tv.setTypeface(Typeface.SERIF, Typeface.BOLD);
		return tv;
	}





	private String bindPayName(List<BillViewDetail> list){
		String str = "";
		for(int index = 0; index < list.size(); index ++){
			if(list.get(index).getMONEY() > 0){
				str += list.get(index).getCIRCLE_MEMBER_NAME();
				str += ",";
			}
		}
		str = AABillApplication.mApplicationContext.getString(R.string.bill_detail_activity_pay_name, str);
		return str;
	}


	private String bindProfitName(List<BillViewDetail> list){
		String str = "";
		for(int index = 0 ; index < list.size(); index ++){
			if(list.get(index).getMONEY() < 0){
				str += list.get(index).getCIRCLE_MEMBER_NAME();
				str += ",";

			}

		}
		str = AABillApplication.mApplicationContext.getString(R.string.bill_detail_activity_profit_name, str);
		return str;
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);

	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
